<template>
    <div class="reg-wrap">
        <div class="ms-reg">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="param" :rules="rules" ref="reg" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="username">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        type="password"
                        placeholder="password"
                        v-model="param.password"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="phone">
                    <el-input
                        type="phone"
                        placeholder="phone"
                        v-model="param.phone"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-phone"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="email">
                    <el-input
                        type="email"
                        placeholder="email"
                        v-model="param.email"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-message"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="commpany">
                    <el-input
                        type="commpany"
                        placeholder="commpany"
                        v-model="param.commpany"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-s-order"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="code">
                    <el-input
                        type="code"
                        placeholder="code"
                        v-model="param.code"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-s-order"></el-button>
                    </el-input>
                </el-form-item>
                <div class="reg-btn">
                <img :src="src" style="cursor:pointer;text-align: center;}" @click="changeimg">
                </div>
                <br><br>
                <div class="reg-btn">
                    <el-button type="primary" @click="submitForm()">注册</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import {reg} from '../../api/api.js'
export default {
    data: function() {
        return {
            src:"http://localhost:8000/code/",
            param: {
                username: '',
                password: '',
                phone:'',
                email:'',
                commpany:'',
                code:'',
                
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
                email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
                commpany: [{ required: true, message: '请输入公司', trigger: 'blur' }],
                code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
            },
        };
    },
    methods: {
        //刷新验证码
        changeimg:function(){
            //定义一个随机数
            this.src = 'http://localhost:8000/code/?random='+(Math.random()*10)
        },
        submitForm() {
            if(this.param.username==''){
                alert('用户名不能为空')
                return false
            }
            if(this.param.password==''){
                alert('密码不能为空')
                return false
            }else if(this.param.password.length < 6){
                alert('密码不能短于6位')
                return false        
            }
            if(this.param.phone==''){
                alert('手机号不能为空')
                return false
            }
            if(!/^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(this.param.phone)){
                alert('手机号格式不正确')
                return false
            }
             if(this.param.email==''){
                alert('手机号不能为空')
                return false
            }
            if(!/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(this.param.email)){
                alert('邮箱格式不正确')
                return false
            }
            if(this.param.code==''){
                alert('用户名不能为空')
                return false
            }
            var params = {
                username:this.param.username,
                password:this.param.password,
                phone:this.param.phone,
                email:this.param.email,
                commpany:this.param.commpany,
                code:this.param.code,
            };
            reg(params).then(res => {
                if(res.code==200){
                    this.$message(res.message)  
                    this.$router.push('/login')  
                }else{
                     this.$message(res.message)  
                }
                
            })
        },
    },
};
</script>

<style scoped>
.reg-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-reg {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.reg-btn {
    text-align: center;
}
.reg-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.reg-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>